package com.ivideo.backend.controller.admin;

import com.ivideo.backend.base.utils.Result;
import com.ivideo.backend.model.system.SysMenu;
import com.ivideo.backend.model.vo.AssginMenuVo;
import com.ivideo.backend.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: ivideo-parent
 * @Name: SysMenuController
 * @Author: Jack_Li
 * @Date: 2024/5/21 14:54
 */

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/menu")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    // 加载
    @ApiOperation(value = "菜单列表")
    @GetMapping(value = "/findNodes")
    public Result<List<SysMenu>> list() {
        List<SysMenu> menuList = sysMenuService.findNodes();
        return Result.success(menuList);
    }

    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody SysMenu sysMenu){
        this.sysMenuService.save(sysMenu);
        return Result.success(null);
    }

    /**
     * 根据id去查询菜单
     * @param id
     * @return
     */
    @ApiOperation("根据id去查询菜单")
    // 根据id去查询菜单
    @GetMapping("/findNodeById/{id}")
    public Result findNodeById(@PathVariable Long id){
        SysMenu sysMenu = this.sysMenuService.getById(id);
        return Result.success(sysMenu);
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return
     */
    @ApiOperation(value = "修改菜单")
    @PostMapping("/updateMenu")
    public Result updateById(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.success(null);
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/removeMenu/{id}")
    public Result removeMenu(@PathVariable Long id) {
        sysMenuService.removeMenuById(id);
        return Result.success(null);
    }

    /**
     * 根据角色获取菜单
     * @param roleId
     * @return
     */
    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
        List<SysMenu> list =  sysMenuService.findSysMenuByRoleId(roleId);
        return Result.success(list);
    }

    /**
     * 给角色分配权限
     * @param assginMenuVo
     * @return
     */
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.success(null);
    }

}
